package cn.yunhe.controller;

import cn.yunhe.entity.Cars;
import cn.yunhe.entity.Emp;
import cn.yunhe.entity.User;
import cn.yunhe.service.CarsService;
import cn.yunhe.util.DateUtil;
import cn.yunhe.util.Result;
import cn.yunhe.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 陈晓萌
 * 2018/9/17 17:25
 */
@RestController
@RequestMapping("/cars")
public class CarsController {

    @Resource
    private CarsService carsService;

    Result result=new Result();

    /**
     * 跳转分页查询页面
     *
     * @return
     */
    @RequestMapping("/find")
    public ModelAndView find() {
        ModelAndView mv = new ModelAndView("cars_list");
        return mv;
    }

    /**
     * 查看车辆信息
     *
     * @param pageSize
     * @param pageNum
     * @param licenseNum
     * @return
     */
    @RequestMapping("/findLikeCars")
    public Map findLikeCars(int pageSize, int pageNum, String licenseNum) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("licenseNum", licenseNum);
        Page page = carsService.findLikeCars(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;
    }

    /**
     * 跳转添加车辆信息页面
     *
     * @return
     */
    @RequestMapping("/addFirst")
    public ModelAndView addFirst() {
        ModelAndView mv = new ModelAndView("cars_add");
        return mv;
    }

    /**
     * 身份证号查询用户是否存在
     *
     * @param userCardId
     * @return
     */
    @RequestMapping("/getUserByCardId")
    public ModelAndView getUserByCardId(String userCardId) {
        ModelAndView mv = new ModelAndView("cars_add");
        User user = carsService.getUserByCardId(userCardId);
        if (user != null) {
            mv.addObject("user", user);
        } else {
            mv.addObject("message", "该用户不存在，请先添加客户信息");
        }
        return mv;
    }

    /**
     * 添加车辆信息
     *
     * @param file
     * @param cars
     * @param session
     * @return
     */
    /*@RequestMapping(value = "/addCars", method = RequestMethod.POST)
    public ModelAndView addCars(@RequestParam MultipartFile file, Cars cars, HttpSession session) {

        String empName = (String) session.getAttribute("empName");
        ModelAndView mv = new ModelAndView("cars_list");
        String fileName = file.getOriginalFilename();
        file.getContentType();
        File dest = new File(fileName);
        cars.setPic(fileName);
        cars.setCarUuid(UUIDGenerator.getUUID());
        cars.setEvaluator(empName);
        cars.setAddTime(DateUtil.date2StringDateTime(new Date()));
        String UserId = cars.getUser().getSuId();
        cars.getUser().setSuId(UserId);
        System.out.println(cars.getUser().getSuId() + "添加的用户的id");
        carsService.addCars(cars);
        try {
            file.transferTo(dest);
            mv.addObject("succ", true);
            mv.addObject("msg", "上传成功");
        } catch (IOException e) {
            mv.addObject("succ", false);
            mv.addObject("msg", "上传失败");
            e.printStackTrace();
        }
        return mv;
    }
*/

    @RequestMapping(value = "/addCars", method = RequestMethod.POST)
    public ModelAndView addCars(@RequestParam MultipartFile file, Cars cars, HttpSession session, HttpServletRequest request) {

        //
        String uploadFilePath=request.getServletContext().getRealPath("/uploadFiles");
        if(file!=null ) {
                File dest=new File(uploadFilePath+File.separator+file.getOriginalFilename());
                if(!dest.isDirectory()){
                    dest.mkdirs();
                }
            String empName = (String) session.getAttribute("empName");
            String fileName = file.getOriginalFilename();
            file.getContentType();
            cars.setPic(fileName);
            cars.setCarUuid(UUIDGenerator.getUUID());
            cars.setEvaluator(empName);
            cars.setAddTime(DateUtil.date2StringDateTime(new Date()));
            String UserId = cars.getUser().getSuId();
            cars.getUser().setSuId(UserId);
            System.out.println(cars.getUser().getSuId() + "添加的用户的id");
            carsService.addCars(cars);
                try {
                    file.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        return new ModelAndView("cars_list");
    }

    /**
     * 查询车牌是否存在
     *
     * @param licensenum
     * @return
     */
    @RequestMapping("/getCarById")
    public boolean getCarById(String licensenum) {
        Cars carslicensenum = carsService.getCarById(licensenum);
        if (carslicensenum != null) {
            return true;
        }
        return false;
    }

    /**
     * 删除车辆信息
     *
     * @param cars
     * @return
     */
    @RequestMapping("/delCars")
    public boolean delCars(Cars cars) {
        return carsService.delCars(cars);
    }

    /**
     * 根据uuid查询车辆信息（修改前查询）
     *
     * @return
     */
    @RequestMapping("/findCarsByUuid")
    public ModelAndView findCarsByUuid(String carUuid) {
        ModelAndView mv = new ModelAndView("cars_update");
        Cars cars = carsService.findCarsByUuid(carUuid);
        mv.addObject("cars", cars);
        return mv;
    }

    /**
     * 修改车辆信息
     * @return
     */
    @RequestMapping("/updateCars")
    public ModelAndView updateCars(Cars cars) {
        ModelAndView mv = new ModelAndView("cars_list");
        carsService.updateCars(cars);
        return mv;
    }
    /*=================================*/
    /**
     * 获取车辆ID
     * @param cars
     * @return
     */
    @RequestMapping("/toCarId")
    public  ModelAndView byCarId(Cars cars){
        Cars c=carsService.getCarsById(cars);
        Map map=new HashMap();
        map.put("data",c);
        return new ModelAndView("cars_update",map);

    }
    /**
     * 修改车辆信息
     * @param cars
     * @return
     */
   /* @RequestMapping("/cars_update")
    public Result editCars(Cars cars){
        carsService.upCarsMessage(cars);
        return new Result();
    }*/

    @RequestMapping("/cars_update")
    public Result editCars(@RequestParam MultipartFile file, Cars cars, HttpSession session, HttpServletRequest request) {

        System.out.println("进入");

        System.out.println(cars.getCarUuid());
        System.out.println(cars.getLicenseNum());

        String uploadFilePath=request.getServletContext().getRealPath("/uploadFiles");
        if(file!=null ) {
            File dest=new File(uploadFilePath+File.separator+file.getOriginalFilename());
            if(!dest.isDirectory()){
                dest.mkdirs();
            }
            Emp emp= (Emp) session.getAttribute("0978752b04234340a4a5fb42831aca1b");
            String fileName = file.getOriginalFilename();
            file.getContentType();
            cars.setPic(fileName);
            cars.setEvaluator("0978752b04234340a4a5fb42831aca1b");
            cars.setAddTime(DateUtil.date2StringDateTime(new Date()));
            carsService.upCarsMessage(cars);
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @RequestMapping("/update_cars")
    public Result updateCarStatus(Cars cars){
        carsService.upCarsStatus(cars);
        return new Result();
    }


    public CarsService getCarsService() {
        return carsService;
    }

    public void setCarsService(CarsService carsService) {
        this.carsService = carsService;
    }

}
